﻿using BCCommon;
using BCCommon.TMS.Order;
using BCEntity.TMS.Order.OrderExtention;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCData.TMS.Order.OrderExtention
{
    public class OrderExtentionData : IOrderExtentionData
    {
        private const string QUERY = @"SELECT A.OrderExtentionId,A.OrderId,A.OrderNo,A.LoadPhotoLeft,A.LoadPhotoFront,A.LoadPhotoBack,A.LoadPhotoShipping,
                                                    A.EndAddress,A.EndProvinceCode,A.EndCityCode,A.EndCountyCode,A.EndLongitude,A.EndLatitude,A.ConsigneeGender,
                                                    A.Consignee,A.ConsigneePhone,A.CreateTime,A.StartTime,A.EndTime,A.PayTime,A.EndIndex,A.AddressType,
                                                    A.TimeOutMinute,A.IsProblemOrder,A.IsTimeout,A.IsCashed,A.CashMode,A.TimeoutClaimPay,A.AgingMinutes,
                                                    A.OrderStatus,A.ProblemOrderRemark,A.CashedTime,A.AcceptanceTime,A.AcceptancePhoto,A.GoodsUnit,
                                                    A.GoodsSku,A.GoodsPhoto,A.GoodsName,A.GoodsWeight,A.GoodsVolume,A.GoodsLength,A.GoodsWidth,A.GoodsHeight,A.GoodsCount,A.GoodsPrice,
                                                    A.AcceptanceSignature,A.EndProvinceName AS ProvinceName,A.EndCityName AS CityName,A.EndCountyName AS CountyName
                                                FROM OrderExtention A";
        private readonly IDatabaseContext databaseContext;

        public OrderExtentionData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        #region 获取插入订单扩展信息
        /// <summary>
        /// 获取插入订单扩展信息
        /// </summary>
        /// <param name="orderExtentionEntities">订单扩展信息列表</param>
        /// <returns></returns>
        public async Task<bool> Add(List<OrderExtentionEntity> orderExtentionEntities)
        {
            return await this.databaseContext.Master.InsertAllAsync(orderExtentionEntities, databaseContext.Transaction) > 0;
        }
        #endregion

        public async Task<IEnumerable<OrderExtentionEntity>> GetList(string orderNo)
        {
            string sql = string.Format("{0} WHERE A.OrderNo=?OrderNo;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            return await databaseContext.Slave.QueryAsync<OrderExtentionEntity>(sql, parameters);
        }
        public async Task<IEnumerable<OrderExtentionEntity>> GetList(long orderId)
        {
            string sql = string.Format("{0} WHERE A.OrderId=?OrderId;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderId, DbType.AnsiString, size: 50);
            return await databaseContext.Slave.QueryAsync<OrderExtentionEntity>(sql, parameters);
        }
        public async Task<IEnumerable<OrderExtentionEntity>> GetNotArrived(string orderNo)
        {
            string sql = string.Format("{0} WHERE A.OrderNo=?OrderNo AND OrderStatus<>'Arrived';", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            return await databaseContext.Slave.QueryAsync<OrderExtentionEntity>(sql, parameters);
        }

        public async Task<bool> UpdateOrderStatusArrived(string orderNo, long orderExtentionId)
        {
            string sql = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus", "EndTime" }, new string[] { "OrderNo", "OrderExtentionId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderStatus", OrderStatus.Arrived.ToString(), DbType.AnsiString);
            parameters.Add("?EndTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            parameters.Add("?OrderExtentionId", orderExtentionId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdateOrderStatusPickup(string orderNo)
        {
            var sqlOne = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus" }, new string[] { "OrderNo" });

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderStatus", OrderStatus.Pickup.ToString(), DbType.AnsiString);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            await this.databaseContext.Master.ExecuteAsync(sqlOne, parameters, databaseContext.Transaction);

            var sqlTwo = SqlCommands.GetUpdateSql("`Orders`", new string[] { "OrderStatus" }, new string[] { "OrderNo" });
            return await this.databaseContext.Master.ExecuteAsync(sqlTwo, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdateOrderStatusTransporting(string orderNo, long orderExtentionId)
        {
            string sql = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus", "StartTime" }, new string[] { "OrderNo", "OrderExtentionId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderStatus", OrderStatus.Transporting.ToString(), DbType.AnsiString);
            parameters.Add("?StartTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            parameters.Add("?OrderExtentionId", orderExtentionId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        public async Task<bool> UpdateOrderStatusTransporting(string orderNo)
        {
            var sqlOne = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus" }, new string[] { "OrderNo" });

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderStatus", OrderStatus.Transporting.ToString(), DbType.AnsiString);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            await this.databaseContext.Master.ExecuteAsync(sqlOne, parameters, databaseContext.Transaction);
            var sqlTwo = SqlCommands.GetUpdateSql("`Orders`", new string[] { "OrderStatus" }, new string[] { "OrderNo" });
            return await this.databaseContext.Master.ExecuteAsync(sqlTwo, parameters, databaseContext.Transaction) > 0;
        }
        public async Task<bool> UpdateOrderStatusAcceptance(string orderNo, long orderExtentionId)
        {
            string sql = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus", "AcceptanceTime" }, new string[] { "OrderNo", "OrderExtentionId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderStatus", OrderStatus.Acceptance.ToString(), DbType.AnsiString);
            parameters.Add("?AcceptanceTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            parameters.Add("?OrderExtentionId", orderExtentionId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }


        //public async Task<bool> UpdatePayerReceiverOrderStatusComplete(string orderNo)
        //{
        //    List<string> sqls = new List<string>();
        //    List<DynamicParameters> dynamicParameters = new List<DynamicParameters>();

        //    sqls.Add(SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus", "PayTime" }, new string[] { "OrderNo" }));
        //    DynamicParameters parameters = new DynamicParameters();
        //    parameters.Add("?OrderStatus", OrderStatus.Complete.ToString(), DbType.AnsiString);
        //    parameters.Add("?PayTime", DateTime.Now, DbType.DateTime);
        //    parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
        //    dynamicParameters.Add(parameters);

        //    parameters = new DynamicParameters();
        //    parameters.Add("?OrderStatus", OrderStatus.Complete.ToString(), DbType.AnsiString);
        //    parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
        //    if (payer == Payer.Receiver)
        //    {
        //        sqls.Add(SqlCommands.GetUpdateSql("`Orders`", new string[] { "OrderStatus", "ConsigneePayTime" }, new string[] { "OrderNo" }));
        //        parameters.Add("?ConsigneePayTime", DateTime.Now, DbType.DateTime);
        //        dynamicParameters.Add(parameters);
        //    }
        //    else
        //    {
        //        sqls.Add(SqlCommands.GetUpdateSql("`Orders`", new string[] { "OrderStatus", "ShippingPayTime" }, new string[] { "OrderNo" }));
        //        parameters.Add("?ShippingPayTime", DateTime.Now, DbType.DateTime);
        //        dynamicParameters.Add(parameters);
        //    }
        //    return await this.databaseContext.TransactionExecuteAsync(sqls, dynamicParameters);
        //}
        public async Task<bool> UpdateOrderStatusCancel(string orderNo)
        {

            var sqlOne = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus" }, new string[] { "OrderNo" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderStatus", OrderStatus.Cancel.ToString(), DbType.AnsiString);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            await this.databaseContext.Master.ExecuteAsync(sqlOne, parameters, databaseContext.Transaction);
            var sqlTwo = SqlCommands.GetUpdateSql("`Orders`", new string[] { "OrderStatus" }, new string[] { "OrderNo" });
            return await this.databaseContext.Master.ExecuteAsync(sqlTwo, parameters, databaseContext.Transaction) > 0;
        }
        public async Task<bool> UpdateOrderStatusPlaceOrder(string orderNo)
        {
            var sqlOne = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus" }, new string[] { "OrderNo" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderStatus", OrderStatus.PlaceOrder.ToString(), DbType.AnsiString);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            await this.databaseContext.Master.ExecuteAsync(sqlOne, parameters, databaseContext.Transaction);
            var sqlTwo = SqlCommands.GetUpdateSql("`Orders`", new string[] { "OrderStatus" }, new string[] { "OrderNo" });
            return await this.databaseContext.Master.ExecuteAsync(sqlTwo, parameters, databaseContext.Transaction) > 0;

        }
        public async Task<bool> UpdateOrderStatusDispatching(string orderNo)
        {

            var sqlOne = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus" }, new string[] { "OrderNo" });

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderStatus", OrderStatus.Dispatching.ToString(), DbType.AnsiString);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            await this.databaseContext.Master.ExecuteAsync(sqlOne, parameters, databaseContext.Transaction);
            var sqlTwo = SqlCommands.GetUpdateSql("`Orders`", new string[] { "OrderStatus" }, new string[] { "OrderNo" });
            return await this.databaseContext.Master.ExecuteAsync(sqlTwo, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdateLoadingGoods(OrderExtentionEntity orderExtentionEntity)
        {
            string sql = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "LoadPhotoLeft", "LoadPhotoFront", "LoadPhotoBack", "LoadPhotoShipping" },
                new string[] { "OrderNo", "OrderExtentionId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?LoadPhotoLeft", orderExtentionEntity.LoadPhotoLeft, DbType.AnsiString, size: 1000);
            parameters.Add("?LoadPhotoFront", orderExtentionEntity.LoadPhotoFront, DbType.AnsiString, size: 1000);
            parameters.Add("?LoadPhotoBack", orderExtentionEntity.LoadPhotoBack, DbType.AnsiString, size: 1000);
            parameters.Add("?LoadPhotoShipping", orderExtentionEntity.LoadPhotoShipping, DbType.AnsiString, size: 1000);
            parameters.Add("?OrderExtentionId", orderExtentionEntity.OrderExtentionId, DbType.Int64);
            parameters.Add("?OrderNo", orderExtentionEntity.OrderNo, DbType.AnsiString, size: 50);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdateCashedTime(string orderNo, long orderExtentionId)
        {
            string sql = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "IsCashed", "CashedTime" }, new string[] { "OrderNo", "OrderExtentionId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?IsCashed", true, DbType.Boolean);
            parameters.Add("?CashedTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?OrderExtentionId", orderExtentionId, DbType.Int64);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdateAcceptanceSignature(OrderExtentionEntity orderExtentionEntity)
        {
            string sql = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "AcceptanceSignature", "AcceptanceTime", "OrderStatus" },
                new string[] { "OrderNo", "OrderExtentionId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?AcceptanceSignature", orderExtentionEntity.AcceptanceSignature, DbType.AnsiString, size: 1000);
            parameters.Add("?AcceptanceTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?OrderStatus", OrderStatus.Acceptance.ToString(), DbType.AnsiString);
            parameters.Add("?OrderExtentionId", orderExtentionEntity.OrderExtentionId, DbType.Int64);
            parameters.Add("?OrderNo", orderExtentionEntity.OrderNo, DbType.AnsiString, size: 50);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdateExtentionProblemOrder(long orderExtentionId, bool noneHandled)
        {
            string sql = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "IsProblemOrder" },
                new string[] { "OrderExtentionId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderExtentionId", orderExtentionId, DbType.Int64);
            parameters.Add("?IsProblemOrder", noneHandled, DbType.Boolean);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdateExtentionProblemHandled(long orderExtentionId)
        {
            string sql = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "ProblemHandled" },
                new string[] { "OrderExtentionId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderExtentionId", orderExtentionId, DbType.Int64);
            parameters.Add("?ProblemHandled", true, DbType.Boolean);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdateOrderExtentionProblemRemark(long orderExtentionId, string remark)
        {
            string sql = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "ProblemOrderRemark", "ProblemHandled" },
                new string[] { "OrderExtentionId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderExtentionId", orderExtentionId, DbType.Int64);
            parameters.Add("?ProblemOrderRemark", remark, DbType.AnsiString, size: 1000);
            parameters.Add("?ProblemHandled", true, DbType.Boolean);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        //public async Task<IEnumerable<ExceptionVehicleEntity>> GetProblemInfo(string orderNo)
        //{
        //    string sql = @"SELECT A.OrderId,A.OrderNo,A.VehicleId,A.VehicleNo,B.ProblemOrderRemark,D.DriverId,E.UserName,E.Avatar,F.CreateTime,F.AttachmentTitle,
        //                F.Attachment,F.AttachmentType,G.VehicleTransportBatchNo,B.OrderExtentionId,B.EndAddress
        //                FROM `Orders` A
        //                INNER JOIN orderextention B ON A.OrderNo=B.OrderNo
        //                INNER JOIN vehicle C ON A.VehicleId=C.VehicleId
        //                INNER JOIN driver D ON C.DriverId=D.DriverId
        //                INNER JOIN `user` E ON D.UserId=E.UserId
        //                INNER JOIN orderextentionattachment F ON A.OrderNo=F.OrderNo
        //                LEFT JOIN vehicletransportbatch G ON A.OrderNo=G.OrderNo
        //                WHERE B.OrderNo=?OrderNo AND B.IsProblemOrder=1
        //                GROUP BY A.OrderId,B.OrderExtentionId";
        //    DynamicParameters parameters = new DynamicParameters();
        //    parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
        //    return await this.databaseContext.QueryAsync<ExceptionVehicleEntity>(sql, parameters);
        //}

        //public async Task<(IEnumerable<ExceptionVehicleEntity>, int)> GetProblemInfo(long vehicleId, int pageIndex, int pageSize)
        //{
        //    string sql = @"SELECT A.OrderId,A.OrderNo,A.VehicleId,A.VehicleNo,B.ProblemOrderRemark,D.DriverId,E.UserName,E.Avatar,F.CreateTime,F.AttachmentTitle,
        //                F.Attachment,F.AttachmentType,G.VehicleTransportBatchNo,B.OrderExtentionId,B.EndAddress
        //                FROM `Orders` A
        //                INNER JOIN orderextention B ON A.OrderNo=B.OrderNo
        //                INNER JOIN vehicle C ON A.VehicleId=C.VehicleId
        //                INNER JOIN driver D ON C.DriverId=D.DriverId
        //                INNER JOIN `user` E ON D.UserId=E.UserId
        //                INNER JOIN orderextentionattachment F ON A.OrderNo=F.OrderNo
        //                LEFT JOIN vehicletransportbatch G ON A.OrderNo=G.OrderNo";
        //    List<string> wheres = new List<string>();
        //    DynamicParameters parameters = new DynamicParameters();
        //    wheres.Add("B.IsProblemOrder=1");
        //    wheres.Add("A.VehicleId=?VehicleId");
        //    parameters.Add("?VehicleId", vehicleId, DbType.Int64, size: 20);
        //    sql = string.Format(@"{0} {1} GROUP BY A.OrderId,F.OrderExtentionAttachmentNo ORDER BY A.CreateTime {4} LIMIT {2},{3}; 
        //                       SELECT Count(1) FROM (
        //                        SELECT A.OrderId
        //                        FROM `Orders` A
        //                        INNER JOIN orderextention B ON A.OrderNo=B.OrderNo
        //                        INNER JOIN vehicle C ON A.VehicleId=C.VehicleId
        //                        INNER JOIN driver D ON C.DriverId=D.DriverId
        //                        INNER JOIN `user` E ON D.UserId=E.UserId
        //                        INNER JOIN orderextentionattachment F ON A.OrderNo=F.OrderNo
        //                        LEFT JOIN vehicletransportbatch G ON A.OrderNo=G.OrderNo
        //                        {1} 
        //                        GROUP BY A.OrderId,F.OrderExtentionAttachmentNo)T",
        //         sql,
        //         wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
        //         (pageIndex - 1) * pageSize,
        //         pageSize,
        //         "DESC");
        //    return await this.databaseContext.QueryPageableListAsync<ExceptionVehicleEntity>(sql, parameters);
        //}

        public async Task<IEnumerable<OrderExtentionEntity>> GetOrderAddressTakeListAsync(AddressType addressType)
        {
            //这里改为所有地点，否则货量太过集中在发货地
            string sql = @"SELECT A.EndProvinceCode,A.EndCityCode,A.EndCountyCode,A.EndLongitude,A.EndLatitude,A.EndProvinceName AS ProvinceName,
                                A.EndCityName AS CityName,A.EndCountyName AS CountyName,COUNT(A.EndCountyCode) AS Quantity
                            FROM OrderExtention A                                               
						    INNER JOIN `Orders` E ON A.OrderNo=E.OrderNo
                            WHERE E.OrderStatus NOT IN ('Cancel','Closed')
                            ORDER BY COUNT(A.EndCountyCode) DESC";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?AddressType", addressType.ToString(), DbType.AnsiString);
            return await this.databaseContext.Slave.QueryAsync<OrderExtentionEntity>(sql, parameters);
        }

        public async Task<bool> UpdateClearLoadPhotosByOrderNoAsync(string orderNo)
        {
            string sql = SqlCommands.GetUpdateSql("OrderExtention",
                new string[] { "LoadPhotoLeft", "LoadPhotoFront", "LoadPhotoBack", "LoadPhotoShipping" }, new string[] { "OrderNo" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?LoadPhotoLeft", null, DbType.AnsiString);
            parameters.Add("?LoadPhotoFront", null, DbType.AnsiString);
            parameters.Add("?LoadPhotoBack", null, DbType.AnsiString);
            parameters.Add("?LoadPhotoShipping", null, DbType.AnsiString);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<OrderExtentionEntity> GetLast(string orderNo)
        {
            string sql = string.Format("{0} WHERE A.OrderNo=?OrderNo ORDER BY A.EndIndex DESC LIMIT 1;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            return await databaseContext.Slave.QueryFirstOrDefaultAsync<OrderExtentionEntity>(sql, parameters);
        }

        public async Task<OrderExtentionEntity> GetFirst(string orderNo)
        {
            string sql = string.Format("{0} WHERE A.OrderNo=?OrderNo ORDER BY A.EndIndex LIMIT 1;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            return await databaseContext.Slave.QueryFirstOrDefaultAsync<OrderExtentionEntity>(sql, parameters);
        }

        public async Task<bool> UpdateOrderStatusArrivedByNo(string orderNo)
        {
            string sql = SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus", "EndTime" }, new string[] { "OrderNo" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderStatus", OrderStatus.Arrived.ToString(), DbType.AnsiString);
            parameters.Add("?EndTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        #region 现付业务--签收订单，修改订单状态及订单扩展信息状态为已完成
        /// <summary>
        /// 现付付业务--签收订单，修改订单状态及订单扩展信息状态为已完成
        /// </summary>
        /// <param name="orderNo">订单编号</param>
        /// <returns></returns>
        public async Task<bool> UpdateCurrentlyPayOrderStatusComplete(string orderNo)
        {
            List<string> sqls = new List<string>();
            List<DynamicParameters> dynamicParameters = new List<DynamicParameters>();

            sqls.Add(SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus" }, new string[] { "OrderNo" }));
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderStatus", OrderStatus.Complete.ToString(), DbType.AnsiString);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            dynamicParameters.Add(parameters);

            sqls.Add(SqlCommands.GetUpdateSql("`Orders`", new string[] { "OrderStatus" }, new string[] { "OrderNo" }));
            dynamicParameters.Add(parameters);

            return await this.databaseContext.Master.TransactionExecuteAsync(sqls, dynamicParameters, databaseContext.Transaction) > 0;

        }
        #endregion

        #region 到付业务--签收订单，修改订单状态及订单扩展信息状态为已完成

        public async Task<bool> UpdateCollectPayOrderStatusComplete(string orderNo)
        {
            List<string> sqls = new List<string>();
            List<DynamicParameters> dynamicParameters = new List<DynamicParameters>();

            sqls.Add(SqlCommands.GetUpdateSql("OrderExtention", new string[] { "OrderStatus" }, new string[] { "OrderNo" }));
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderStatus", OrderStatus.Complete.ToString(), DbType.AnsiString);
            parameters.Add("?OrderNo", orderNo, DbType.AnsiString, size: 50);
            dynamicParameters.Add(parameters);

            sqls.Add(SqlCommands.GetUpdateSql("`Orders`", new string[] { "OrderStatus" }, new string[] { "OrderNo" }));
            dynamicParameters.Add(parameters);

            return await this.databaseContext.Master.TransactionExecuteAsync(sqls, dynamicParameters, databaseContext.Transaction) > 0;
        }
        #endregion
    }
}
